#include "Block.h"
#include "Game.h"

QPen Block::sPenLeftTop1(Const::COLOR_LEFT_TOP_1);
QPen Block::sPenLeftTop2(Const::COLOR_LEFT_TOP_2);
QPen Block::sPenRightBottom1(Const::COLOR_RIGHT_BOTTOM_1);
QPen Block::sPenRightBottom2(Const::COLOR_RIGHT_BOTTOM_2);

/**
 * 构造方法。
 */
Block::Block() :
    mLine(3),
    mRow(-1),
    mShape(ONE) {
    int red = rand() % 256;
    int green = rand() % 256;
    int blue = rand() % 256;
    mColor = QColor(red, green, blue);
}

/**
 * 绘制自身。
 */
void Block::draw() {
    mDrawer.begin(mGameBoardBitmap);

    for (int i = 0; i < Const::BLOCK_GRID_COUNT_H; i++) {
        for (int j = 0; j < Const::BLOCK_GRID_COUNT_V; j++) {
            if (!mBlocks[i][j].mUsed) {
                continue;
            }

            mDrawer.fillRect(Const::GRID_SIZE * (mLine + j),
                Const::GRID_SIZE * (mRow + i), Const::GRID_SIZE, Const::GRID_SIZE, mColor);
            Block::drawEdge(&mDrawer, Const::GRID_SIZE * (mLine + j), Const::GRID_SIZE * (mRow + i));
        }
    }

    mDrawer.end();
    mGameBoard->update();
}

/**
 * 画矩形的边框，使矩形具有立体效果。
 * @param drawer    绘图者
 * @param left, top 矩形的左上角坐标
 */
void Block::drawEdge(QPainter* drawer, int left, int top) {
    int right = left + Const::GRID_SIZE - 1;
    int bottom = top + Const::GRID_SIZE - 1;

    drawer->setPen(sPenLeftTop1);
    drawer->drawLine(left, top, right, top);
    drawer->drawLine(left, top, left, bottom);

    drawer->setPen(sPenLeftTop2);
    drawer->drawLine(left + 1, top + 1, right - 1, top + 1);
    drawer->drawLine(left + 1, top + 1, left + 1, bottom - 1);

    drawer->setPen(sPenRightBottom1);
    drawer->drawLine(right, top, right, bottom);
    drawer->drawLine(left, bottom, right, bottom);

    drawer->setPen(sPenRightBottom2);
    drawer->drawLine(right - 1, top + 1, right - 1, bottom - 1);
    drawer->drawLine(left + 1, bottom - 1, right - 1, bottom - 1);
}

/**
 * 进入游戏区域。
 * @param gameBoard         游戏区域
 * @param gameBoardBitmap   游戏区域后台缓冲位图
 */
void Block::enterGameBoard(QWidget* gameBoard, QPixmap* gameBoardBitmap) {
    mGameBoard = gameBoard;
    mGameBoardBitmap = gameBoardBitmap;
    this->draw();
}

/**
 * 进入预览区域。
 * @param previewBitmap 预览区域位图
 */
void Block::enterPreview(QPixmap* previewBitmap) {
    int x = (Const::PREVIEW_WIDTH - Const::GRID_SIZE * 3) / 2;
    int y = Const::PREVIEW_HEIGHT / 2 - Const::GRID_SIZE * 2;

    // 遍历所有格，跳过无效的，画有效的
    mDrawer.begin(previewBitmap);
    for (int i = 0; i < Const::BLOCK_GRID_COUNT_H; i++) {
        for (int j = 0; j < Const::BLOCK_GRID_COUNT_V; j++) {
            if (!mBlocks[i][j].mUsed) {
                continue;
            }

            mDrawer.fillRect(x + Const::GRID_SIZE * j,
                y + Const::GRID_SIZE * i, Const::GRID_SIZE, Const::GRID_SIZE, mColor);
            Block::drawEdge(&mDrawer, x + Const::GRID_SIZE * j, y + Const::GRID_SIZE * i);
        }
    }
    mDrawer.end();
}

/**
 * 擦除矩形。
 * @param rect  矩形
 */
void Block::eraseRect(QRect* rect) {
    mDrawer.begin(mGameBoardBitmap);
    mDrawer.fillRect(*rect, Const::GAME_BOARD_COLOR);
    mDrawer.end();
}

/**
 * 下落。
 * @return 总是 true
 */
bool Block::moveDown() {
    this->erase();
    mRow++;
    this->draw();

    return true;
}

/**
 * 左移。
 */
void Block::moveLeft() {
    this->erase();
    mLine--;
    this->draw();
}

/**
 * 右移。
 */
void Block::moveRight() {
    this->erase();
    mLine++;
    this->draw();
}
